﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Classes
{
   
    public class Searcher
    {
        IList<City> airports;
        IList<City> visited;
        IGraphImpl graph;

        public IGraphImpl Graph
        {
            get { return graph; }
            set { graph = value; }
        }
      
        public Searcher()
        {
            visited = new List<City>();
            graph = new GraphImpl();          
        
        }
        public void MakeAGraph(IList<City> airports, IList<Flight> flights)
        {
            foreach(City c in airports)
            {
                Graph.AddCity(c);
                
            }
            foreach(Flight f in flights)
            {
                City theCityFrom = f.From;
                City theCityTo = f.To;
                Graph.AddFlight(theCityFrom, theCityTo, f.NoOfSeats, f.Id, f.DepartureTime, f.ArrivalTime, f.PriceOfTheOneSeat, f.Company);
            }
        }
        
        
        public IGraphImpl ReturnGraph()
        {
            return Graph;
        }

        public Connection Dijkstra(City startCity, City destinationCity) //cost efficient
        {
            try
            {

                Connection a = graph.DijsktraInvoker(startCity, destinationCity);
                int numberOfFlightChanges = 0;
                int totalPrice = 0;

                a.DepartureCity = startCity;
                a.DestinationCity = destinationCity;

                a.DepartureTime = a.FlightList()[0].DepartureTime;

                a.ArrivalTime = a.FlightList().Last().ArrivalTime;


                foreach (Flight g in a.FlightList())
                {
                    totalPrice += g.PriceOfTheOneSeat;
                    numberOfFlightChanges++;
                }
                a.TotalPrice = totalPrice;
                a.NumberOfFlightChange = numberOfFlightChanges;


                return a;
            }
            catch (Exception)
            {

                return null;
            }
        }


        public Connection DijkstraTime(City startCity, City destinationCity) //time efficient
        {
            try
            {
                Connection a = graph.DijsktraInvokerTime(startCity, destinationCity);
                int numberOfFlightChanges = 0;
                int totalPrice = 0;



                a.DepartureCity = startCity;
                a.DestinationCity = destinationCity;




                a.DepartureTime = a.FlightList()[0].DepartureTime;

                a.ArrivalTime = a.FlightList().Last().ArrivalTime;
                foreach (Flight g in a.FlightList())
                {
                    totalPrice += g.PriceOfTheOneSeat;
                    numberOfFlightChanges++;
                }
                a.TotalPrice = totalPrice;
                a.NumberOfFlightChange = numberOfFlightChanges - 1;
                return a;

            }
            catch (Exception)
            {

                return null;
            }
        }


        public void fillTheConnections(City from, City to, ListOfConnections conList)
        {


            foreach (Connection a in conList.GetConnectionList())
            {
               
                int numberOfFlightChanges = 0;
                int totalPrice = 0;

                a.DepartureCity = from;
                a.DestinationCity = to;
              
                a.DepartureTime = a.FlightList()[0].DepartureTime;
                
                a.ArrivalTime = a.FlightList().Last().ArrivalTime;

                foreach (Flight g in a.FlightList())
                {
                    totalPrice += g.PriceOfTheOneSeat;
                    numberOfFlightChanges++;
                }

                a.TotalPrice = totalPrice;
                a.NumberOfFlightChange = numberOfFlightChanges - 1;  
                

            }


        }
    }
}